package com.zuimei.zuimei.fragments;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zuimei.zuimei.R;
import com.zuimei.zuimei.activities.Item1DescActivity;
import com.zuimei.zuimei.adapters.Item1RecycleViewItemAdapter;
import com.zuimei.zuimei.base.BaseFragment;
import com.zuimei.zuimei.beans.FindAppItem1Bean;
import com.zuimei.zuimei.jsonutil.ParseFindAppJson;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by Jogger on 2017/1/16.
 * 发现应用viewpager内fragment
 */

public class FindAppItem1Fragment extends BaseFragment implements SwipeRefreshLayout
        .OnRefreshListener {

    @Bind(R.id.rv_item)
    RecyclerView rv_item;
    @Bind(R.id.srl_refresh)
    SwipeRefreshLayout srl_refresh;//下拉刷新
    private List<FindAppItem1Bean> mAppContentList;
    private List<Object> mAppList;
    private Item1RecycleViewItemAdapter mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.rv_find_app_item;
    }

    @Override
    public void loadData() {
        srl_refresh.setOnRefreshListener(this);
        String jsonData = getJsonData();
        mAppList = new ArrayList<>();
        mAppContentList = ParseFindAppJson.getItem1List(jsonData);
        rv_item.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new Item1RecycleViewItemAdapter(mActivity);
        for (int i = 0; i < mAppContentList.size(); i++) {
            FindAppItem1Bean findAppItem1Bean = mAppContentList.get(i);
            List<FindAppItem1Bean.AppsBean> apps = findAppItem1Bean.getApps();
            mAppList.add(findAppItem1Bean.getLabel_date());//将标签放在集合第一个
            for (int j = 0; j < apps.size(); j++) {
                mAppList.add(apps.get(j));
            }
        }
        rv_item.setAdapter(mAdapter);
        mAdapter.loadData(mAppList);
        mAdapter.setOnItemClickListener(new Item1RecycleViewItemAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, FindAppItem1Bean.AppsBean appBean) {
                startNewActivity(Item1DescActivity.class, "app", appBean);
            }
        });
    }

    /**
     * 获取json数据
     */
    private String getJsonData() {
        String result = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            is = mActivity.getResources().getAssets().open("selectjson");
            baos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int ret;
            while ((ret = is.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, ret);
            }
            result = baos.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    @Override
    public void onRefresh() {
        mAdapter.notifyDataSetChanged();
        srl_refresh.setRefreshing(false);
    }
}
